import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";

// 导入所有router
const metaRouters = import.meta.glob("./modules/*.ts", { eager: true }) as any; //import.meta.globEager已被启用，该方法是替换方案
// * 处理路由表
export const routerArray: RouteRecordRaw[] = [];
Object.keys(metaRouters).forEach((item) => {
  Object.keys(metaRouters[item]).forEach((key: any) => {
    routerArray.push(...metaRouters[item][key]);
  });
});



const routes: RouteRecordRaw[] = [
  ...routerArray,
  // {
  //   path: "/",
  //   redirect: { name: "home" },
  // },
  {
    path: "/",
    name: "home",
    meta: { title: "首页", keepAlive: true, requiresAuth: true },
    component: () => import("@/views/home/index.vue"),
  },
  {
    path: "/gobang",
    name: "gobang",
    meta: { title: "五子棋", keepAlive: true, requiresAuth: true, id: "66aa19e57616eb6b8557846f" },
    component: () => import("@/views/gobang/index.vue"),
  },
  {
    path: "/shoot",
    name: "shoot",
    meta: { title: "射击游戏", keepAlive: true, requiresAuth: true, id: "66aa4066f5b3657af47d5a45" },
    component: () => import("@/views/shoot/index.vue"),
  },
  {
    path: "/2048",
    name: "2048",
    meta: { title: "2048", keepAlive: true, requiresAuth: true, id: "66aa4238f5b3657af47d5a4c" },
    component: () => import("@/views/_2048/index.vue"),
  },
  {
    path: "/flappy_bird",
    name: "flappy_bird",
    meta: { title: "飞翔的小鸟", keepAlive: true, requiresAuth: true, id: "66b1267cc57f82fbc42e68f8" },
    component: () => import("@/views/flappyBird/index.vue"),
  },

  {
    // 找不到路由重定向到404页面
    path: "/:pathMatch(.*)",
    redirect: { name: "404" },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
  // 切换页面，滚动到最顶部
  scrollBehavior: () => ({
    left: 0,
    top: 0,
  }),
});

export default router;
